#include "qclog/qcfileappender.h"
#include <QFile>

void QcLog::QcFileAppender::run()
{
    QFile log_file(file_pathname_);
    bool is_open = log_file.open(QIODevice::WriteOnly | QIODevice::Append);

    while(is_activation_ && !is_restart_)
    {
        QString one_message;
        {
            QMutexLocker lock(&msg_list_mutex_);
            if(message_list_.size() > 0)
            {
                one_message = message_list_.takeFirst();
            }
        }

        if(!one_message.isEmpty() && is_open)
        {
            QTextStream out(&log_file);
            out.setCodec("UTF-8");
            out << one_message;

#ifdef Q_OS_WIN32
            out << "\r\n";
#endif
        }

        msleep(10);
    }

    log_file.close();
}
